package com.CGFrame.struts.bean.category;

import java.util.Iterator;
import java.util.List;

import com.CGFrame.struts.POJOs.Category;
import com.CGFrame.struts.POJOs.Catemodel;
import com.CGFrame.struts.POJOs.Catemodeltime;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class CatemodeltimeBean {
	private static ThreadLocal<ProjectOperate> tl = new ThreadLocal<ProjectOperate>();
	{
		tl.set(ProjectOperateFactory.getprojectOperate());
	}

	public Integer addCatemodeltime(Integer modelId,Integer cateId,Integer hours) {
		ProjectOperate pop = (ProjectOperate) tl.get();
		List list=pop.Query("Catemodeltime", "catemodel,"+modelId, null, null);
		for(Iterator iter=list.iterator();iter.hasNext();){
			Catemodeltime cmt=(Catemodeltime)iter.next();
			if(cmt.getCategory().getCategoryId().equals(cateId)){
				return null;
			}
		}
		Category cate=(Category)pop.UniqueQuery("Category", "categoryId,"+cateId);
		Catemodel model=(Catemodel)pop.UniqueQuery("Catemodel", "cateModelId,"+modelId);
		
		Catemodeltime cmt=new Catemodeltime();
		cmt.setCategory(cate);
		cmt.setCatemodel(model);
		cmt.setHours(hours);
		pop.saveObject(cmt);
		return cmt.getCateModleTimeId();
	}
	
	public void changTime(String timeId,Integer hours){
		ProjectOperate pop = (ProjectOperate) tl.get();
		Catemodeltime c=(Catemodeltime)pop.UniqueQuery("Catemodeltime","cateModleTimeId,"+timeId);
		c.setHours(hours);
		pop.saveObject(c);
	}
	
	public List getModelsById(String modelId){
		ProjectOperate pop = (ProjectOperate) tl.get();
		List list=pop.Query("Catemodeltime","catemodel,"+modelId, null, null);
		return list;
	}
	
	public void delCatemodeltime(Integer timeId){
		ProjectOperate pop = (ProjectOperate) tl.get();
		pop.deleteObject("Catemodeltime", timeId);
	}
}
